﻿using System;
using System.Text;
using System.Web;
using EPiServer;
using EPiServer.Core;
using EPiServer.Web;

namespace SocialPack.Extensions
{
    public static class PageDataExtensions
    {
        /// <summary>
        /// Get the pages external URL
        /// </summary>
        /// <param name="page"></param>
        /// <returns>The pages external URL</returns>
        public static string GetExternalUrl(this PageData page)
        {
            string result = string.Empty;

            if (page.IsEPiServerPage())
            {
                var url = new UrlBuilder(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + page.LinkURL);

                if (UrlRewriteProvider.IsFurlEnabled)
                {
                    Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8);
                    result = url.ToString();
                }
                else
                {
                    result = url.ToString();
                }
            }

            return result;
        }

        public static bool IsEPiServerPage(this PageData page)
        {
            return page != null && page.PageLink != null && page.PageLink.ID > 0;
        }
    }
}